package org.anderground.editor.edits;

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoableEdit;

public abstract class AbstractAction<T, V> extends Action {

	private T target;
	private V value;

	public AbstractAction(T target, V value) {
		this.target = target;
		this.value = value;
	}

	@Override
	final UndoableEdit perform() throws ActionException {
		return new Edit();
	}

	@SuppressWarnings("serial")
	private class Edit extends AbstractUndoableEdit {

		public Edit() {
			swapValues();
		}

		@Override
		public void undo() throws CannotUndoException {
			swapValues();
		}

		@Override
		public void redo() throws CannotRedoException {
			swapValues();
		}

		private void swapValues() {
			V temp = value;
			value = getCurrentValue();
			setCurrentValue(temp);
		}

	}

	protected abstract V getCurrentValue();

	protected abstract void setCurrentValue(V value);

	/**
	 * @return the target
	 */
	protected final T getTarget() {
		return target;
	}
}
